import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
//线程池的实现
/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: WHY
 * Date: 2023-03-25
 * Time: 14:46
 */
class MyThreadPool{
    //阻塞队列存放任务
    private BlockingQueue<Runnable> queue=new LinkedBlockingQueue<>();
    //将任务放到队列
    public void submit(Runnable runnable) throws InterruptedException {
        queue.put(runnable);
    }
    //实现固定线程数的线程池
    public MyThreadPool(int n){
        for(int i=0;i<n;i++){
            Thread t=new Thread(()->{
                try {

                    while(true) {
                        Runnable runnable = queue.take();
                        runnable.run();
                    }

                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }

            });
            t.start();
        }
        }
    }




public class ThreadingDemo2 {
    public static void main(String[] args) throws InterruptedException {
        MyThreadPool  pool=new MyThreadPool(10);
        for(int i=0;i<1000;i++){
            int number=i;
            pool.submit(new Runnable() {
                @Override
                public void run() {
                    System.out.println("hello"+number);
                }
            });
        }

    }
}
